//
//  CTSetViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/29.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTSetViewController.h"
#import "CTCertificationViewController.h"
#import "CTAboutUsViewController.h"
#import "CTPersonalProfileViewController.h"
#import "CTTabBarController.h"

//header
#import "CTHeader.h"

//conig
#import "CTConfig.h"

//view
#import "CTSetView.h"

//logic
#import "CTMessageLogic.h"

//function
#import "CTDebugManager.h"
#import "CTLogin.h"
#import "CTActivitorView.h"
#import "CTDeviceFunction.h"
#import "CTAppLanguage.h"
#import "CTAppInfo.h"
#import "CTGenRequestData.h"
#import "CTDataGenerator.h"
#import "CTTextManager.h"
#import "MobClick.h"
#import "CTRemindView.h"




@interface CTSetViewController ()<CTSetViewDelegate>
{
    CTSetView * setView;
    
    CTActivitorView * activitorView;
    
    CTMessageLogic * messageLogic;
}
@end

@implementation CTSetViewController
@synthesize levelsType;

- (void)dealloc
{
    messageLogic = nil;
    
    [setView removeFromSuperview];
    setView = nil;
    
    [activitorView removeFromSuperview];
    activitorView = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTSetViewController Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        messageLogic = [[CTMessageLogic alloc] init];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //统计
    [MobClick beginLogPageView:@"CTSetViewController"];
    if ([CTLogin isUserLoginState] == YES) {
        setView.isLogin = YES;
    }else{
        setView.isLogin = NO;
    }
    setView.notication = NO;
    setView.cacheText = [CTAppInfo appCacheCalculate];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTSetViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    setView = [[CTSetView alloc] init];
    setView.delegate = self;
    
    [self.view addSubview:setView];
    
    NSString * stwitchStr = [CTDataGenerator generateMessageSwitch];
    if ([[CTTextManager shareInstance] isBlankString:stwitchStr] == YES) {
        setView.notication = YES;
    }else{
        if ([stwitchStr isEqualToString:@"1"]) {
            setView.notication = YES;
        }else{
            setView.notication = NO;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - action -

-(void)sendNotiSwitchRequest:(NSString *)switchType
{
    [messageLogic messageSwitchRequest:[CTGenRequestData genRequestMessageSwitchItem:switchType] successCompletionHandler:^{
        
    } failedHandler:^{
        
    }];
}


#pragma mark - set view delegate -
//设置个人资料
-(void)setPersonalProfileBtnAction:(CTSetView *)view
{
    CTPersonalProfileViewController * personalProfileVC = [[CTPersonalProfileViewController alloc] init];
    [self.navigationController pushViewController:personalProfileVC animated:YES];
}
//返回按钮事件回调
- (void)setViewBackButton:(CTSetView *) view
{
    if (levelsType == CTSetViewControllerLevelsFirst) {
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC showTabbarWithAnimate:YES];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
//意见反馈
- (void)setViewOpinionBtnAction:(CTSetView *)view
{
    [self joinGroup1:@"hello" key:@"hello"];
}
//成为认证画师
- (void)setViewCertificationBtnAction:(CTSetView *) view
{
    [self joinGroup:@"hello" key:@"hello"];
//    CTCertificationViewController * certificationVC = [[CTCertificationViewController alloc] init];
//    [self.navigationController pushViewController:certificationVC animated:YES];
}
//评论应用
- (void)setViewCommentBtnAction:(CTSetView *) view
{
    [CTDeviceFunction jumpToAppStore:CT_CONFIG_CHINA_APPSTORE_ADDRESS];
}
//邀请好友
- (void)setViewInviteBtnAction:(CTSetView *) view
{
    
}
//通知开关
- (void)setViewNoticationBtnAction:(CTSetView *) view noticationBool:(BOOL)noticBool
{
    if (noticBool == YES) {
        //开
        [self sendNotiSwitchRequest:@"1"];
    }else{
        //关
        [self sendNotiSwitchRequest:@"0"];
    }
}
//清理缓存
- (void)setViewClearCacheBtnAction:(CTSetView *) view
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:[CTAppLanguage localizedStringForKey:@"setView_hint_clearCache" withValue:nil withTable:@"Language"] delegate:self cancelButtonTitle:[CTAppLanguage localizedStringForKey:@"setView_hint_sure" withValue:nil withTable:@"Language"] otherButtonTitles:[CTAppLanguage localizedStringForKey:@"setView_hint_cancel" withValue:nil withTable:@"Language"], nil];
    alertView.tag = 1000;
    [alertView show];
//    [[EGOCache currentCache] clearCache];
}
//关于
- (void)setViewAboutBtnAction:(CTSetView *) view
{
    CTAboutUsViewController * aboutUsVC = [[CTAboutUsViewController alloc] init];
    [self.navigationController pushViewController:aboutUsVC animated:YES];
}
//退出
- (void)setViewExitBtnAction:(CTSetView *) view
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:[CTAppLanguage localizedStringForKey:@"setView_hint_extendBtn" withValue:nil withTable:@"Language"] delegate:self cancelButtonTitle:[CTAppLanguage localizedStringForKey:@"setView_hint_sure" withValue:nil withTable:@"Language"] otherButtonTitles:[CTAppLanguage localizedStringForKey:@"setView_hint_cancel" withValue:nil withTable:@"Language"], nil];
    alertView.tag = 1001;
    [alertView show];
}

#pragma mark AlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag ==1000)
    {
        if (buttonIndex == 0)
        {
            [CTAppInfo clearAppCache];
            setView.cacheText = [CTAppInfo appCacheCalculate];
            
            //发送清理缓存通知（首界面更新数据）
            [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_CLEAR_CACHE object:nil];
        }
    }
    else if(alertView.tag == 1001)
    {
        if (buttonIndex == 0)
        {
            activitorView = [[CTActivitorView alloc] init];
            [activitorView showActivitorViewOn:self.view];
            
            [CTLogin exitAccountWithsSuccessCompletionHandler:^{
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [activitorView removeActivitorView];
                    activitorView = nil;
                    
                    CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
                    [tabbarVC showTabbarWithAnimate:YES];
                    [tabbarVC selectTabbarIndex:0];
                    
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    
                });
                
            } failedHandler:^{
                
            }];

        }
    }
   
    
}
//加入群
- (BOOL)joinGroup1:(NSString *)groupUin key:(NSString *)key{
    NSString *urlStr = CT_CONFIG_USER_FEEDBACK_QQ_GROUP_ADDRESS;
    NSURL *url = [NSURL URLWithString:urlStr];
    if([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url];
        return YES;
    }
    else
    {
        UIAlertView * shareAlertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"没有安装QQ或QQ版本过低" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [shareAlertView show];
        return NO;
    }
}

//画师群
- (BOOL)joinGroup:(NSString *)groupUin key:(NSString *)key{
    NSString *urlStr = CT_CONFIG_PAINTER_QQ_GROUP_ADDRESS;
    NSURL *url = [NSURL URLWithString:urlStr];
    
    if([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url];
        return YES;
    }
    else
    {
        UIAlertView * shareAlertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"没有安装QQ或QQ版本过低" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [shareAlertView show];
        return NO;
    }
}


@end
